package phapp.service.custom.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import cn.org.rapid_framework.page.Page;
import cn.org.rapid_framework.page.PageRequest;
import javacommon.base.*;
import phapp.model.custom.*;
import phapp.model.qfd.Custdiscuss;
import phapp.dao.custom.*;
import phapp.service.common.QfdException;
import phapp.service.custom.impl.*;
import phapp.service.custom.*;

/**
 * Title: t_attention_data 业务处理层 Description: spring事务实现层,调用Dao层取数据 Copyright:
 * Copyright (c) 2012-10 Company:
 * 
 * @author you name
 * @version 1.0
 */

@Component("attentionManager")
@Transactional
public class AttentionManagerImpl extends BaseManager<Attention, java.lang.Integer> implements AttentionManager
{

   /** 定义Dao */
   private AttentionDao attentionDao;
   private CustomManager customManager;

   /** 增加setXXXX()方法,spring就可以通过autowire自动设置对象属性 */
   public void setAttentionDao(AttentionDao dao)
   {
      this.attentionDao = dao;
   }

   /** 获取Dao */
   public EntityDao getEntityDao()
   {
      return this.attentionDao;
   }

   public void setCustomManager(CustomManager customManager)
   {
      this.customManager = customManager;
   }

   /**
    * <p>
    * 分页查询Attention<br>
    * 
    * @param PageRequest
    *           pr
    * @return Page
    * @author
    */
   @Transactional(readOnly = true)
   public Page findByPageRequest(PageRequest pr)
   {
      return attentionDao.findByPageRequest(pr);
   }

   /**
    * <p>
    * 批量删除 Attention<br>
    * 
    * @param Attention
    *           u
    * @return Boolean
    * @author
    */
   @Transactional(readOnly = true)
   public Boolean removeByIdArry(Attention u)
   {
      Boolean result = false;
      int i = -1, size = u.getIdList().size();
      i = attentionDao.removeByIdArry(u);
      if(i == size)
      {
         result = true;
      }
      else
      {
         throw new RuntimeException();
      }
      return result;
   }

   public Map customAttention(JSONObject jsonData)
   {
      Custom customData = null, objectData = null;
      int nStatus = 0, nOPFlag = 0;
      List<Custom> listQuery = null;
      Attention attentionData = null;
      List<Attention> listData = null;
      String CustAccount = "", ObjCustID = "";
      Map<String, Object> mapResult = new HashMap<String, Object>();
      Map<String, Object> mapQuery = new HashMap<String, Object>();

      try
      {
         CustAccount = jsonData.getString("user");
         ObjCustID = jsonData.getString("attentionuserid");
         
         mapQuery.clear();
         mapQuery.put("custAccount", CustAccount);
         listQuery = customManager.findList(mapQuery);
         if(listQuery.size() == 0 || CustAccount == null || CustAccount.length() == 0)
            throw new QfdException(2000);
         if(listQuery.size() > 1)
            throw new QfdException(2008);
         else
            customData = listQuery.get(0);
         
         objectData = customManager.getById(new Integer(ObjCustID));
         if(objectData == null)
            throw new QfdException(2000);
//         mapQuery.clear();
//         mapQuery.put("custAccount", ObjCustID);
//         listQuery = customManager.findList(mapQuery);
//         if(listQuery.size() == 0 || CustAccount == null || CustAccount.length() == 0)
//            throw new QfdException(2000);
//         if(listQuery.size() > 1)
//            throw new QfdException(2008);
//         else
//            objectData = listQuery.get(0);
         
         nOPFlag = new Integer(jsonData.getString("attentionstatus"));
         if(nOPFlag == 1)
         {
            mapQuery.clear();
            mapQuery.put("custId", customData.getCustId());
            mapQuery.put("attentionCustid", ObjCustID);
            listData = attentionDao.findList(mapQuery);
            
            //不存在关注数据
            if(listData.size() == 0)
            {
               attentionData = new Attention();
               attentionData.setCustId(customData.getCustId());
               attentionData.setAttentionCustid(objectData.getCustId());
               attentionData.setAttentionStatus(1);
               save(attentionData);
               
               //增加被关注用户的关注数量
               objectData.setAttentionCount(objectData.getAttentionCount()+1);
               customManager.update(objectData);
            }
         }
         else
         {
            mapQuery.clear();
            mapQuery.put("custId", customData.getCustId());
            mapQuery.put("attentionCustid", jsonData.getString("attentionuserid"));
            listData = attentionDao.findList(mapQuery);
            
            if(listData.size() == 0)
               ;
            else if(listData.size() == 1)
            {
               attentionData = listData.get(0);
               attentionDao.deleteById(attentionData.getDataId());
               
               objectData.setAttentionCount(objectData.getAttentionCount()+1);
               customManager.update(objectData);
            }
            else if(listData.size() > 1)
            {
               ;
            }
            else
            {
            }
         }
         
         nStatus = 1000;
      }
      catch(QfdException eQfd)
      {
         nStatus = eQfd.getErrorCode();
      }
      catch(Exception eLogin)
      {
         nStatus = 10000;
         eLogin.printStackTrace();
      }
      finally
      {
         mapResult.put("statusCode", nStatus);
         if(nStatus == 1000)
            mapResult.put("message", "操作成功！");
         else if(nStatus == 2000)
            mapResult.put("message", "用户不存在！");
         else if(nStatus == 2008)
            mapResult.put("message", "存在多个用户！");
         else
            mapResult.put("message", "操作失败！");
      }
      
      return mapResult;
   }

   public Map queryAttentionData(JSONObject jsonData)
   {
      int nStatus = 0;
      String CustAccount = "";
      Custom customData = null;
      List<Custom> listQuery = null;
      List<Attention> listData = null;
      Map<String, Object> mapResult = new HashMap<String, Object>();
      Map<String, Object> mapQuery = new HashMap<String, Object>();

      try
      {
         mapQuery.clear();
         CustAccount = jsonData.getString("user");
         mapQuery.put("custAccount", CustAccount);
         listQuery = customManager.findList(mapQuery);
         if(listQuery.size() == 0 || CustAccount == null || CustAccount.length() == 0)
            throw new QfdException(2000);
         if(listQuery.size() > 1)
            throw new QfdException(2008);
         
         mapQuery.clear();
         customData = listQuery.get(0);
         mapQuery.put("custId", customData.getCustId());
         mapQuery.put("attentionCustid", jsonData.getString("attentionuserid"));
         listData = attentionDao.findList(mapQuery);
         
         //不存在关注数据
         if(listData.size() == 0)
            mapResult.put("attentionstatus", 0);
         else
            mapResult.put("attentionstatus", 1);
         
         nStatus = 1000;
      }
      catch(QfdException eQfd)
      {
         nStatus = eQfd.getErrorCode();
      }
      catch(Exception eLogin)
      {
         nStatus = 10000;
         eLogin.printStackTrace();
      }
      finally
      {
         mapResult.put("statusCode", nStatus);
         if(nStatus == 1000)
            mapResult.put("message", "操作成功！");
         else if(nStatus == 2000)
            mapResult.put("message", "用户不存在！");
         else if(nStatus == 2008)
            mapResult.put("message", "存在多个用户！");
         else
            mapResult.put("message", "操作失败！");
      }
      
      return mapResult;      
   }
   
   public Map getAttentionData(JSONObject jsonData)
   {
      int nStatus = 0, i = 0;
      String CustAccount = "";
      Custom customData = null;
      JSONArray arrayData = null;
      JSONObject jsonAttention = null;
      Attention attentionData = null;
      List<Custom> listQuery = null;
      List<Attention> listData = null;
      Map<String, Object> mapResult = new HashMap<String, Object>();
      Map<String, Object> mapQuery = new HashMap<String, Object>();

      try
      {
         mapQuery.clear();
         CustAccount = jsonData.getString("user");
         mapQuery.put("custAccount", CustAccount);
         listQuery = customManager.findList(mapQuery);
         if(listQuery.size() == 0 || CustAccount == null || CustAccount.length() == 0)
            throw new QfdException(2000);
         if(listQuery.size() > 1)
            throw new QfdException(2008);
         
         mapQuery.clear();
         customData = listQuery.get(0);
         mapQuery.put("custId", customData.getCustId());
         listData = attentionDao.findList(mapQuery);
         
         arrayData = new JSONArray();
         for(i=0; i<listData.size(); i++)
         {
            attentionData = listData.get(i);
            jsonAttention = new JSONObject();
            
            jsonAttention.put("dataid", attentionData.getDataId());
            jsonAttention.put("custid", attentionData.getCustId());
            jsonAttention.put("custname", attentionData.getCustName());
            jsonAttention.put("objectcustid", attentionData.getAttentionCustid());
            jsonAttention.put("objectcustname", attentionData.getAttentionCustName());
            jsonAttention.put("objectimgurl", attentionData.getObjectImgurl());
            jsonAttention.put("attentiondate", attentionData.getCreateDateString());
            
            arrayData.add(jsonAttention);
            jsonAttention = null;
         }
         
         mapResult.put("attentionlist", arrayData);
         nStatus = 1000;
      }
      catch(Exception eLogin)
      {
         nStatus = 10000;
         eLogin.printStackTrace();
      }
      finally
      {
         mapResult.put("statusCode", nStatus);
         if(nStatus == 1000)
            mapResult.put("message", "获取关注成功！");
         else
            mapResult.put("message", "获取关注失败！");
      }
      
      return mapResult;
   }
   
   
}
